package yc.day0109.lottery.service;


import yc.day0109.lottery.constants.UserStatus;
import yc.day0109.lottery.dao.UserDao;
import yc.day0109.lottery.po.User;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.logging.Logger;


public class UserMgr {
    private Logger logger = Logger.getLogger(String.valueOf(UserMgr.class));
    public void showUserInfo(UserDao userDao, Scanner sc){
        System.out.println("请输入中签编号(不输默认查询所有)：");
        String markNo = sc.nextLine();
        Map<String,Object> map = new HashMap<>();
        if(markNo != null && !"".equals(markNo)){
            map.put("markNo",markNo);
        }
        List<User> userList = userDao.queryUserByType(map);
        System.out.println("用户编号\t\t用户姓名\t\t中签编码\t\t持照状态\t\t手机号码\t\t开户日期");
        for (User u : userList) {
            System.out.println(u.getId() + "\t\t" + u.getUserName() + "\t\t" + u.getMarkNo() + "\t\t" +
                    UserStatus.getUserStatusByCode(u.getStatus()).getValue() + "\t\t" +
                    u.getPhone() + "\t\t" + u.getOpenDate());
        }
    }
    public static String getCharRandomNum(int length) {
        StringBuffer valSb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 字符串
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;  // 取得大写字母还是小写字母
                valSb.append((char) (choice + random.nextInt(26)));
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                // 数字
                valSb.append(String.valueOf(random.nextInt(10)));
            }
        }
        return valSb.toString();
    }
    public void addUserInfo(UserDao userDao,Scanner sc){
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        User user = new User();
        System.out.println("请输入用户姓名：");
        user.setUserName(sc.nextLine());
        System.out.println("请输入手机号码：");
        String phone = sc.nextLine();
        user.setPhone(phone);
        user.setMarkNo(getCharRandomNum(10));
        user.setOpenDate(date.format(formatter));
        int addRes = userDao.addUser(user);
        System.out.println(addRes > 0 ? "添加成功" : "添加失败");
    }
    public void lottery(UserDao userDao) {
        Map<String,Object> map = new HashMap<>();
        List lottery = new ArrayList<>();
        List lotterys = new ArrayList();
        List<User> usersLists = userDao.queryUserByType(map);
        for (User u:usersLists) {
            lottery.add(u.getMarkNo());
        }
        System.out.println("请输入摇取中签的数量:");
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        int len = lottery.size();
        System.out.println("摇取中签的"+number+"个号码为：");
        do {
            int num = 0;
            Object str = lottery.get((int) Math.round(Math.random()*(len - 1)));
            for (int i = 0;i<lotterys.size();i++){
                if (str.equals(lotterys.get(i))){
                    continue;
                }else{
                    num++;
                }
            }
            if (num == lotterys.size()){
                lotterys.add(str);
                System.out.println(str);
            }
        }while (lotterys.size()<number);
    }
}
